package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.TeacherRoutineParam;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.PointsReqVO;
import com.smfx.yxt.reqVo.TeacherRoutineReqVO;
import com.smfx.yxt.service.ITeacherPointService;
import com.smfx.yxt.service.ITeacherRoutineParamService;
import com.smfx.yxt.service.ITeacherRoutineService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.TeacherRoutineView;
import com.smfx.yxt.vo.PointsQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.TEACHER_POINTS)
@Controller
public class TeacherPointsController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(TeacherPointsController.class);
	
	@Autowired
	private ITeacherRoutineParamService teacherRoutineParamService;
	@Autowired
	private ITeacherRoutineService teacherRoutineService;
	@Autowired
	private ITeacherPointService teacherPointService;
	
	@RequestMapping(value = {"","/"})
	public String index(Model model) {
		AppUser appUser = getAppUser();
		if(StringUtil.isExistPosition(appUser.getTeacher().getPosition(), "5")) {
			model.addAttribute("isAdmin", true);
		}
		model.addAttribute("isJudgeTeacher", teacherRoutineParamService.
				isJudgeTeacher(appUser.getTeacher().getId()));
		return "teacherPoints/index";
	}
	
	/**
	 * 教师常规登记
	 */
	@RequestMapping("register")
	public String register() {
		return "teacherPoints/register";
	}
	
	/**
	 * 类别列表
	 * @return
	 */
	@RequestMapping("typeList")
	public String typeList(Model model) {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<TeacherRoutineParam> list = teacherRoutineParamService.selectList(vo);
		model.addAttribute("list", list);
		return "teacherPoints/typeList";
	}
	/**
	 * 类别详情
	 * @param model
	 * @param typeId
	 * @return
	 */
	@RequestMapping("type/detail/{typeId}")
	public String typeDetail(Model model, @PathVariable Integer typeId) {
		model.addAttribute("typeId", typeId);
		return "teacherPoints/typeDetail";
	}
	
	/**
	 * 常规详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("routine/detail/{id}")
	public String routineDetail(Model model, @PathVariable Integer id){
		model.addAttribute("detail", teacherRoutineService.selectViewById(id));
		return "teacherPoints/routineDetail";
	}
	
	@RequestMapping("personal")
	public String personalPoints() {
		return "teacherPoints/personal";
	}
	
	@RequestMapping("school")
	public String schoolPoints() {
		return "teacherPoints/school";
	}
	
	@RequestMapping("type/detail/query")
	@ResponseBody Object typeDetailQuery(Integer typeId) {
		return teacherRoutineParamService.selectById(typeId);
	}
	
	/**
	 * 类别修改
	 * @param record
	 * @return
	 */
	@RequestMapping("type/addUpdate")
	@ResponseBody Object typeUpdate(TeacherRoutineParam record) {
		try {
			teacherRoutineParamService.addUpdate(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---typeUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("register/power")
	@ResponseBody Object registerPower() {
		AppUser appUser = getAppUser();
		return teacherRoutineParamService.selectByTeacherId(appUser.getTeacher().getId());
	}
	
	/**
	 * 教师常规登记提交
	 * @param vo
	 * @return
	 */
	@RequestMapping("register/submit")
	@ResponseBody Object registerSubmit(TeacherRoutineReqVO vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setJudgeTeacherId(appUser.getTeacher().getId());
			vo.setTermId(getCurrentTerm().getId());
			teacherRoutineService.addTeacherRoutine(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--registerSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("totalPoint")
	@ResponseBody Object totalPoint(Integer keyId) {
		PointsReqVO vo = new PointsReqVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setKeyId(keyId);
		return teacherPointService.selectTotalPointForTeacher(vo);
	}
	
	@RequestMapping("routine/pageQuery")
	@ResponseBody Object routinePageQuery(PointsQueryVO vo) {
		try {
			Page<TeacherRoutineView> page = new Page<TeacherRoutineView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("评价日期", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<TeacherRoutineView> list = teacherRoutineService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---routinePageQuery error={}", e);
		}
		return null;
	}
}
